import fs from "fs-extra";
import path from "path";
import { toolchain } from "../toolchain.js";

export class FileTypeScanner {
    public async go(): Promise<void> {
        const collection: string[] = [];
        await this.statFileTypes(toolchain.opts.projectRoot, collection);
        console.log(collection.sort().join(', '));
    }

    private async statFileTypes(dir: string, collection: string[]): Promise<void> {
        const files = await fs.readdir(dir);
        for (const f of files) {
            if (f.startsWith('.')) continue;
            const file = path.join(dir, f);
            const fstat = await fs.stat(file);
            if (fstat.isFile()) {
                const extName = path.extname(f);
                if (!collection.includes(extName)) {
                    collection.push(extName);
                }
            } else {
                await this.statFileTypes(file, collection);
            }
        }
    }
}
